const router = require('koa-router')()
const { User } = require('../models/index')
router.prefix('/users')

router.get('/', function (ctx, next) {
  ctx.body = 'this is a users response!'
})
// 用户
router.get('/bar1', function (ctx, next) {
  ctx.body = 'this is a users/bar response'
})

//用户登录
router.post('/login', async (ctx, next) => {
  let { realName, pwd, role } = ctx.request.body
  await User.find({ realName, pwd, role }).then(res => {
    // 注意 boolean([])=>true
    if (res.length) { //如果返回不为空=>查找成功  反之为查找失败
      ctx.body = {
        code: 200,
        msg: '查找成功',
        data: res
      }
    } else {
      ctx.body = {
        code: 300,
        msg: '查找失败'
      }
    }



  }).catch(err => {
    ctx.body = {
      code: 400,
      msg: '查找时出现异常'
    }
  })
})

//用户查找 和角色查找
router.get('/find', async (ctx, next) => {
  let type = JSON.stringify(ctx.query) != '{}' ? ctx.query : {}
  await User.find(type).then(res => {
    // 注意 boolean([])=>true
    if (res.length) {
      ctx.body = {
        code: 200,
        msg: '查找成功',
        data: res,
        total: res.total,
      }
    } else {
      ctx.body = {
        code: 300,
        msg: '查找失败'
      }
    }
  }).catch(err => {
    ctx.body = {
      code: 400,
      msg: '查找时出现异常'
    }
  })
})


//模糊分页查询
router.post('/getPage', async (ctx, next) => {
  let page = +ctx.request.body.pageNum || 1
  let perPage = +ctx.request.body.pageSize || 6
  let name = ctx.request.body.name


  //查询总条数
  const total = await User.countDocuments({ name: { $regex: name, $options: '$i' } })
  await User.find({ name: { $regex: name, $options: '$i' } }).skip(perPage * (page - 1)).limit(perPage).then(res => {
    ctx.body = {
      code: 200,
      msg: '分页查找',
      data: res,
      // res.total就可以拿到总条数
      uTotal: total
    }
  }).catch(err => {
    console.log(err)
  })
})




//跑腿人员的模糊分页查询
router.post('/runerGetPage', async (ctx, next) => {
  let page = +ctx.request.body.pageNum || 1
  let perPage = +ctx.request.body.pageSize || 6
  let name = ctx.request.body.name
  let role = ctx.request.body.role


  //查询总条数
  const total = await User.countDocuments({ name: { $regex: name, $options: '$i' }, role: role })
  await User.find({ name: { $regex: name, $options: '$i' }, role: role }).skip(perPage * (page - 1)).limit(perPage).then(res => {
    ctx.body = {
      code: 200,
      msg: '分页查找',
      data: res,
      // res.total就可以拿到总条数
      uTotal: total
    }
  }).catch(err => {
    console.log(err)
  })
})


//批量删除
router.post('/deleteMany', async (ctx, next) => {
  // let itemCheckLists = ctx.request.body.
  let itemCheckLists = ctx.request.body.userIds
  console.log(itemCheckLists)
  await User.deleteMany({ id: { $in: itemCheckLists } }).then(res => {
    ctx.body = {
      code: 200,
      msg: '删除成功',
      data: res
    }
  }).catch(err => {
    ctx.body = {
      code: 400,
      msg: '删除时出现异常'
    }
  })

})

//修改跑腿者的工资
router.post('/runerUpdate', async (ctx, next) => {
  let params = ctx.request.body
  await User.updateOne({ id: params.id }, {
    baseSalary: params.baseSalary,
    reward: params.reward,
    allowance: params.allowance,
    fine: params.fine,
  }).then(res => {
    ctx.body = {
      code: 200,
      msg: '修改成功',
      resut: res
    }

  }).catch(err => {
    ctx.body = {
      code: 400,
      msg: '添加时出现异常'
    }
  })
})











module.exports = router
